package utils.db.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import pojo.EUserType;
import pojo.User;
import utils.db.api.IUserFetcher;
import utils.db.mapper.IMapper;
import utils.db.mapper.Mapper;

public class UserFetcher extends DBOperation implements IUserFetcher {
	private static Logger logger = LoggerFactory.getLogger(UserFetcher.class);
	private User user;
	private IMapper mapper;
	private List<User> users;

	public UserFetcher() {
		user = new User();
		mapper = new Mapper();
	}

	public User getUser(String email, String password) {

		try {
			pstmt = getConnector().prepareStatement(GET_USER_BY_EMAIL_PASSWORD);
			pstmt.setString(1, email);
			pstmt.setString(2, password);
			rs = pstmt.executeQuery();
			if (rs.next())
				user = mapper.userMapp(rs);
			pstmt.close();
			return user;
		} catch (SQLException e) {
			logger.error("Error during fetch user");
		}

		return null;
	}

	public List<User> getUsers() {

		try {
			pstmt = getConnector().prepareStatement(GET_ALL_USERS);
			rs = pstmt.executeQuery();
			users = mapper.usersMapp(rs);
			pstmt.close();
		} catch (SQLException e) {
			logger.error("Error during fetch users");
		}
		logger.info("Added user " + user.toString());
		return users;
	}

	@Override
	public List<User> getUsers(EUserType type) {
		List<User> users = new ArrayList<User>();
		try {
			pstmt = getConnector().prepareStatement(GET_USERS_BY_TYPE);
			pstmt.setString(1, type.name());
			rs = pstmt.executeQuery();
			users = mapper.usersMapp(rs);
			pstmt.close();
		} catch (SQLException e) {
			logger.error("Error during fetch users");
		}
		logger.info("Retrieve user " + user.toString());
		return users;
	}
	
	@Override
	public List<User> getUsers(EUserType type, EUserType typer) {
		List<User> users = new ArrayList<User>();
		try {
			pstmt = getConnector().prepareStatement(GET_USERS_BY_TYPES);
			pstmt.setString(1, type.name());
			pstmt.setString(2, typer.name());
			rs = pstmt.executeQuery();
			users = mapper.usersMapp(rs);
			pstmt.close();
		} catch (SQLException e) {
			logger.error("Error during fetch users");
		}
		logger.info("Retrieve user " + user.toString());
		return users;
	}

	@Override
	public boolean checkUserExist(String email) {
		try {
		
			pstmt = getConnector().prepareStatement(GET_USER_BY_EMAIL);
			pstmt.setString(1, email);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public User getUserById(int id) {
		try {
			pstmt = getConnector().prepareStatement(GET_USER_BY_ID);
			pstmt.setInt(1, id);
			rs = pstmt.executeQuery();
			if (rs.next())
				user = mapper.userMapp(rs);
			pstmt.close();
			return user;
		} catch (SQLException e) {
			logger.error("Error during fetch user");
		}

		return null;

	}

	public static void main(String[] args) {
		IUserFetcher fetcher = new UserFetcher();
		for (User user : fetcher.getUsers(EUserType.CLIENT)) {
			System.out.println(user.toString());
		}
	}

}
